package leetcode_1100;

import java.util.Comparator;
import java.util.PriorityQueue;

public class TwoCityScheduling_1030 {
	public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {
		int[][] res = new int[R*C][2];
        PriorityQueue<int[]> queue = new PriorityQueue<int[]>(new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				int dis1 = Math.abs(o1[0]-r0)+Math.abs(o1[1]-c0);
				int dis2 = Math.abs(o2[0]-r0)+Math.abs(o2[1]-c0);
				return dis1-dis2;
			}
		});
        for(int i=0;i<R;i++) {
        	for(int j=0;j<C;++j) {
        		int [] now = new int[] {i,j};
        		queue.offer(now);
        	}
        }
        int i = 0;
        while(!queue.isEmpty()) {
        	res[i++] = queue.poll();
        }
        return res;
    }
	
	
}
